package servlets;

import java.io.IOException;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import delegate.BusinessDelegate;
import dtos.ArticuloDTO;

/**
 * Servlet implementation class OrdenarArticulos
 */
@WebServlet("/OrdenarArticulos")
public class OrdenarArticulos extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrdenarArticulos() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String columna = request.getParameter("columna");
		String tipo = request.getParameter("tipo");
		String orden = request.getParameter("orden");
		String operacion = request.getParameter("operacion");

		try {
			String resp = "";
			List<ArticuloDTO> arts = BusinessDelegate.getInstancia().obtenerPorOrden(tipo, columna, orden);
			
			for(ArticuloDTO art: arts){
				
				if(art.getTipo().equalsIgnoreCase("Ninio")){
					resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
							"<td>" + art.getDescripcion() + "</td>"+
							"<td>" + art.getMarca() + "</td>"+
							"<td>" + art.getPrecio() + "</td>"+
							"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;

					if(operacion.equalsIgnoreCase("compra"))
						resp += "<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
						"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
						"<button type=\"button\" style=\"border: 0; background: transparent\"  onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "') \">" +
						"<img src=\"img/agregar.png\" /> </button> </td> " ;		 
					
					resp += "</tr>";
				}
				
				if(art.getTipo().equalsIgnoreCase("Moda")){
					resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
							"<td>" + art.getDescripcion() + "</td>"+
							"<td>" + art.getMarca() + "</td>"+
							"<td>" + art.getPrecio() + "</td>"+
							"<td>" + art.getColor()+ "</td> " +
							"<td>" + art.getTalle()+ "</td> " +
							"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;					
					if(operacion.equalsIgnoreCase("compra"))
						resp+= "<td align=\"center\"> <input id=\"" + art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
						"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
						"<button type=\"button\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "') \">" +
						"<img src=\"img/agregar.png\" /> </button> </td>" ;		 

					resp += "</tr>";
				}
					

				if(art.getTipo().equalsIgnoreCase("Electrodomestico")){
					resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
						"<td>" + art.getDescripcion() + "</td>"+
						"<td>" + art.getMarca() + "</td>"+
						"<td>" + art.getPrecio() + "</td>"+
						"<td>" + art.getFichaTecnica()+ "</td> " +
						"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;	
					if(operacion.equalsIgnoreCase("compra"))
						resp += "<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
						"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
						"<button type=\"button\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "')\" style=\"border: 0; background: transparent\">" +
						"<img src=\"img/agregar.png\" /> </button> </td>" ;		 
					
					resp += "</tr>";
				}
				
				if(art.getTipo().equalsIgnoreCase("Mueble")){
					resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
						"<td>" + art.getDescripcion() + "</td>"+
						"<td>" + art.getMarca() + "</td>"+
						"<td>" + art.getPrecio() + "</td>"+
						"<td>" + art.getFichaTecnica()+ "</td> " +
						"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;
					
					if(operacion.equalsIgnoreCase("compra"))
						resp+= "<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
						"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
						"<button type=\"button\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "')\">" +
						"<img src=\"img/agregar.png\" /> </button> </td> " ;		 
						
					resp += "</tr>";
					
				}
				
			}
			response.getWriter().write(resp);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}

}
